namespace RuoVea.WeighIns.Web.Controllers;

[Authorize(Roles = "None")]
public class StatisticsController : Controller
{
    private readonly WeighStatisticsService _statisticsService;

    public StatisticsController(WeighStatisticsService statisticsService)
    {
        _statisticsService = statisticsService;
    }

    public IActionResult index()
    {
        ViewBag.Title = "数据可视化与统计分析";
        return View();
    }

    /// <summary>
    /// 获取增强的体重趋势数据（支持时间范围和时段筛选）
    /// </summary>
    /// <param name="days">查询天数</param>
    /// <param name="meridiemFilter">时段筛选（可选）</param>
    /// <returns></returns>
    [HttpGet("Statistics/enhanced-trend/{days?}")]
    public async Task<RestfulResult<List<EnhancedWeightTrendPoint>>> GetEnhancedWeightTrendAsync(
        int days = 30,
        int? meridiemFilter = null)
    {
        return await _statisticsService.GetEnhancedWeightTrendAsync(days, meridiemFilter);
    }

    /// <summary>
    /// 获取当前数据快照
    /// </summary>
    /// <returns></returns>
    [HttpGet("Statistics/current-snapshot")]
    public async Task<RestfulResult<CurrentWeightSnapshot>> GetCurrentWeightSnapshotAsync()
    {
        return await _statisticsService.GetCurrentWeightSnapshotAsync();
    }

    /// <summary>
    /// 获取历史体重极值统计
    /// </summary>
    /// <returns></returns>
    [HttpGet("Statistics/extremes")]
    public async Task<RestfulResult<WeightExtremes>> GetWeightExtremesAsync()
    {
        return await _statisticsService.GetWeightExtremesAsync();
    }

    /// <summary>
    /// 获取周期性变化统计
    /// </summary>
    /// <returns></returns>
    [HttpGet("Statistics/periodic-change")]
    public async Task<RestfulResult<PeriodicChange>> GetPeriodicChangeAsync()
    {
        return await _statisticsService.GetPeriodicChangeAsync();
    }

    /// <summary>
    /// 获取累计变化与平均变化率
    /// </summary>
    /// <returns></returns>
    [HttpGet("Statistics/change-rate")]
    public async Task<RestfulResult<ChangeRate>> GetChangeRateAsync()
    {
        return await _statisticsService.GetChangeRateAsync();
    }

    /// <summary>
    /// 根据时段获取体重统计
    /// </summary>
    /// <param name="meridiem">时段ID</param>
    /// <returns></returns>
    [HttpGet("Statistics/period-specific/{meridiem}")]
    public async Task<RestfulResult<PeriodSpecificStat>> GetPeriodSpecificStatAsync(int meridiem)
    {
        return await _statisticsService.GetPeriodSpecificStatAsync(meridiem);
    }

    /// <summary>
    /// 获取身体指标统计
    /// </summary>
    /// <param name="metricType">指标类型</param>
    /// <param name="days">查询天数</param>
    /// <returns></returns>
    [HttpGet("Statistics/metric/{metricType}/{days?}")]
    public async Task<RestfulResult<MetricStat>> GetMetricStatAsync(string metricType, int days = 30)
    {
        return await _statisticsService.GetMetricStatAsync(metricType, days);
    }
}